﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using CoreClasses;
using Mvvm;
using LayoutPlanning.Views.Machines;
using RelayCommand = LayoutPlanning.Helpers.RelayCommand;

namespace LayoutPlanning.Model
{
    public class StationLoaderModel : BindableBase
    {
        public StationLoaderModel()
        {
            Reset();
        }

        public RelayCommand CreateNewStation => new RelayCommand(() =>
        {
            Stations.Add(new Station() {ControlType = ControlType.Square, SizeX = 1000, SizeY = 1000});
        });

        public ObservableCollection<Station> Stations { get; } = new ObservableCollection<Station>();

        public void Reset()
        {
            var ss = new Station[]
            {
                new Model.Station()
                {
                    Name = "MD13",
                    Position = new Point(0, 0),
                    NegativeInfluence = 0,
                    ControlType = ControlType.Custom,
                    CustomControl = new SerializableFrameworkElement(new Machine1()),

                    SizeX = 2000,
                    SizeY = 1000
                },
                new Station()
                {
                    Name = "ME123",
                    Position = new Point(0, 0),
                    NegativeInfluence = 0,
                    ControlType = ControlType.Custom,
                CustomControl = new SerializableFrameworkElement(new Machine2()),
                    SizeX = 2000,
                    SizeY = 2000
                },
                new Station()
                {
                    Name = "HC123",
                    Position = new Point(0, 0),
                    NegativeInfluence = 0,
                    SizeX = 2000,
                    SizeY = 1300,
                    ControlType = ControlType.Custom,
                    CustomControl = new SerializableFrameworkElement(new Machine3())
                }
            };
#if DEBUG
            var service = new CollaborationService.ServiceContractClient("BasicHttpsBinding_IServiceContractDebug");
#else
            var service = new CollaborationService.ServiceContractClient("BasicHttpsBinding_IServiceContract");
#endif

            //foreach (var station in ss)
            //{
            //    service.AddGeneralStation(new CollaborationService.Station()
            //    {
            //        Color = station.NormalColorString,
            //        Control = station.CustomControl.GetContractData().Binary,
            //        ControlType = station.ControlType.ToString(),
            //        Name = station.Name,
            //        SyncId = station.SyncID,
            //        SizeX = station.SizeX,
            //        SizeY = station.SizeY

            //    });
            //}

            var genelarStations = service.GetGeneralStations();
            Stations.Clear();

            foreach (var station in genelarStations)
            {
                ControlType Type;
                SerializableFrameworkElement element = null;
                if (!ControlType.TryParse(station.ControlType, true, out Type))
                    Type = ControlType.Square;
                if (Type == ControlType.Custom)
                {
                    try
                    {
                        SerializableFrameworkElement.Data data = new SerializableFrameworkElement.Data()
                        {
                            Binary = station.Control
                        };
                        element = data.GetObject();
                    }
                    catch { }
                }

                Stations.Add(new Station()
                {
                    Name = station.Name,
                    Position = new Point(0, 0),
                    SizeX = station.SizeX,
                    SizeY = station.SizeY,
                    ControlType = Type ,
                    CustomControl = element,
                    NormalColorString = station.Color,
                    SyncedText = "Update",
                    SyncID = station.SyncId
                    
                
                });
            }

            
  
        }
    }
}